package org.jeecg.modules.yw.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.yw.entity.ContractInfo;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.ContractInfoService;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.utils.CommonUtils;
import org.jeecg.modules.yw.utils.DESUtil;
import org.jeecg.modules.yw.utils.DateUtils;
import org.jeecg.modules.yw.utils.UserUtil;
import org.jeecg.modules.yw.vo.ContractInfoVo;
import org.jeecg.modules.yw.vo.EnterPriseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

@RestController
@Api(tags = "合同管理")
@RequestMapping("/app/contractInfos")
public class ContractInfoController {
    @Resource
    private ContractInfoService contractInfoService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @ApiOperation(value = "添加合同")
    @PostMapping("/addContractInfo")
    @Transactional
    public Result addContractInfo(@RequestBody ContractInfoVo contractInfoVo, HttpServletRequest request) throws Exception {
        ContractInfo contractInfo = new ContractInfo();
        contractInfo.setId(CommonUtils.getID());
        BeanUtils.copyProperties(contractInfoVo, contractInfo);
        contractInfo.setContractEntryTime(DateUtils.getCurrentDate());
        contractInfo.setCreator(UserUtil.getUserId(request,sysBaseAPI));
        contractInfo.setCreatetime(DateUtils.getCurrentDate());
        boolean re = contractInfoService.save(contractInfo);
        if (!re) {
            return Result.error("添加合同失败");
        }
        if (contractInfoVo.getPics()!= null) {
            contractInfoService.saveBatchContractInfoPics(contractInfoVo.getPics(), contractInfo.getId());
        }
        return Result.success("添加合同成功");
    }
    @ApiOperation(value = "修改合同")
    @PostMapping("/editContractInfo")
    @Transactional
    public Result editContractInfo(@RequestBody ContractInfoVo contractInfoVo) {
        ContractInfo contractInfo = new ContractInfo();
        BeanUtils.copyProperties(contractInfoVo, contractInfo);
        contractInfo.setContractEntryTime(DateUtils.getCurrentDate());
        boolean re= contractInfoService.updateById(contractInfo);
        if (!re) {
            return Result.error("修改合同失败");
        }
        if (contractInfoVo.getPics() != null) {
            contractInfoService.deleteContractInfoPics(contractInfo.getId());
            contractInfoService.saveBatchContractInfoPics(contractInfoVo.getPics(), contractInfo.getId());
        }
        return Result.success("修改合同成功");
    }
    @ApiOperation(value = "删除合同")
    @DeleteMapping("/deleteContractInfo")
    @Transactional
    public org.jeecg.common.api.vo.Result deleteContractInfo(String id) {
        boolean re = contractInfoService.deleteContractInfoPics(id);
        contractInfoService.removeById(id);
        if (!re) {
            return org.jeecg.common.api.vo.Result.error("删除合同失败");
        }
        return org.jeecg.common.api.vo.Result.ok("删除合同成功");
    }
    @ApiOperation(value = "获取合同根据enterPriseId")
    @GetMapping("/getContractInfoById")
    @ResponseBody
    public Result getContractInfoById(@RequestParam(required = false) String id,HttpServletRequest request) throws Exception{
        String token=request.getHeader("token");
        String enterpriseId=DESUtil.decryptCBCId(token);
        QueryWrapper<ContractInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_id",enterpriseId);
        ContractInfo one = contractInfoService.getOne(queryWrapper);
        HashMap<String, Object> map =new HashMap<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(one.getContractEndTime());
        // 加一年
        Date oneYearLater = calendar.getTime();
        Date currentTime = new Date();
        // 计算剩余天数
        long remainingDays = (oneYearLater.getTime() - currentTime.getTime()) / (24 * 60 * 60 * 1000);
        // 将剩余时间放入 map
        map.put("remainingDay", remainingDays);
        return Result.success(map);
    }


}
